package cn.bonoon.controllers;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.batatasframework.chart.ChartDefinition;
import org.batatasframework.chart.data.ChartData;
import org.batatasframework.chart.data.ChartExportResolver;
import org.batatasframework.chart.data.ChartTableResolver;
import org.batatasframework.common.ParameterReader;
import org.batatasframework.web.RequestParameterReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.core.ChartManagedService;
import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.exceptions.PrivilegeException;
import cn.bonoon.kernel.exceptions.UnloginException;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.support.services.UserService;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.controllers.PanelParser;
import cn.bonoon.kernel.web.controllers.StandardLayoutParser;
import cn.bonoon.kernel.web.models.PanelModel;

@Controller
@RequestMapping("s/cm/{catalog}/{key}/!{mid}/")
public class ChartManagedController extends AbstractController{

	@Autowired private ModuleManager moduleManager;
	@Autowired private UserService userService;
	@Autowired private ChartManagedService chartManagedService;

	@RequestMapping(value = "load.do", method = {GET, POST} )
	public String load(HttpServletRequest request, 
			Model model,
			@PathVariable("catalog") String catalog, 
			@PathVariable("key") String key, 
			@PathVariable("mid") String mid) {
		
		model.addAttribute("layout", "layout-empty.vm");
		try{
			LogonUser user = getUser();
			ParameterReader reader = new RequestParameterReader(request);
			ChartData data = chartManagedService.load(user, catalog, key, reader);
			ChartTableResolver tableResolver = new ChartTableResolver(data);
			tableResolver.resolve();
			
			model.addAttribute("chart", tableResolver);
			return "plugs-chart/index-data";
		}catch (Exception e) {
			return getError(e);
		}
	}

	@RequestMapping(value = "export.do", method = POST)
	public void export(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("catalog") String catalog, 
			@PathVariable("key") String key, 
			@PathVariable("mid") String mid) {
		try{
			LogonUser user = getUser();
			ParameterReader reader = new RequestParameterReader(request);
			ChartData data = chartManagedService.load(user, catalog, key, reader);
			XSSFWorkbook workbook = new XSSFWorkbook();
			ChartExportResolver extportResolver = new ChartExportResolver(data, workbook);
			extportResolver.resolve();
			extportResolver.write(response);
			
		}catch (Exception e) {
			log.error("导出失败！", e);
		}
	}
	
	@RequestMapping(value = "index.do", method = GET)
	public ModelAndView get(HttpServletRequest request, 
			@PathVariable("catalog") String catalog, 
			@PathVariable("key") String key, 
			@PathVariable("mid") String mid) {
		try {
			LogonUser user = getUser();
			PanelParser parser = new StandardLayoutParser(moduleManager, userService);
			PanelEvent event = parser.parse(mid, request, user);
			PanelModel model = event.getModel();
			ChartDefinition definition = chartManagedService.load(user, catalog, key);
			model.addObject("queryPath", definition.getQueryPath());
			model.addObject("exportable", definition.isExportable());
			event.setVmPath("plugs-chart/index");
			return event.execute();
		} catch (PrivilegeException t) {
			//没有访问的权限
			return errorPanel(t);
		} catch (UnloginException t) {
			//进入当前功能需要用户登录，如果当前操作者还没有登录，则会直接跳转到登录界面
			return errorPanel(t);
		} catch (Throwable t) {
			//这里抛出操作异常
			return errorPanel(t);
		}
	}
}
